package chat.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.util.ArrayList;
import java.util.List;

import chat.adapter.SearchRoomAdapter;
import chat.dto.Rooms;
import chat.network.http.JoinRoom;
import chat.network.http.SearchRoom;
import inter.Delegate;
import inter.ResponseStatus;
import utils.TimeUtil;

/**
 * 查找房间
 * Created by Administrator on 2016/3/29.
 */
public class SearchRoomActivity extends UIHandler.BaseActivity {
    EditText et_roomName;
    Button btn_search;
    ListView lv_rooms;
    List<Rooms> rooms = new ArrayList<Rooms>();
    SearchRoomAdapter searchRoomAdapter = new SearchRoomAdapter();
    {
        searchRoomAdapter.setRooms(rooms);
    }

    SearchRoom searchRoom = new SearchRoom();
    Delegate searchRoomResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            SearchRoom.ResponseSearchRooms responseSearchRooms = gson.fromJson(response, SearchRoom.ResponseSearchRooms.class);
            if (responseSearchRooms.getStatus().equals(ResponseStatus.SUCCESS))
            {
                rooms.clear();
                rooms.addAll(responseSearchRooms.getRooms());
                searchRoomAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };
    {
        searchRoom.setDelegate(searchRoomResponse);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchroom);
        initview();
    }

    private void initview() {
        et_roomName = (EditText) findViewById(R.id.id_et_roomName);
        btn_search = (Button) findViewById(R.id.id_btn_search);
        lv_rooms = (ListView) findViewById(R.id.id_lv_rooms);
        lv_rooms.setAdapter(searchRoomAdapter);

        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchRoom();
            }
        });

        lv_rooms.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final Rooms room = (Rooms) view.getTag(R.id.id_search_room);
                new AlertDialog.Builder(SearchRoomActivity.this).setTitle("加入房间?")
                        .setMessage("选择加入，加入房间").setPositiveButton("加入", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                JoinRoom joinRoom = new JoinRoom();
                                joinRoom.setDelegate(new Delegate() {
                                    @Override
                                    public void onResponse(String response) {
                                        if (!response.startsWith("{")) return;
                                        Gson gson = new Gson();
                                        JoinRoom.ResponseJoinRoom responseJoinRoom = gson.fromJson(response, JoinRoom.ResponseJoinRoom.class);
                                        if (responseJoinRoom.getStatus().equals(JoinRoom.JoinRoomStatus.SUCCESS))
                                        {
                                            Toast.makeText(MyApplication.share(),"加入成功",Toast.LENGTH_SHORT).show();
                                        }
                                        else if (responseJoinRoom.getStatus().equals(JoinRoom.JoinRoomStatus.JOINED))
                                        {
                                            Toast.makeText(MyApplication.share(),"已经加入",Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onErrorResponse(VolleyError error) {

                                    }
                                });

                                JoinRoom.UserJoinRoom userJoinRoom = new JoinRoom.UserJoinRoom();
                                userJoinRoom.setJoin_Name(MyApplication.getOwnUser().getUser_RealName());
                                userJoinRoom.setUser_ID(Long.parseLong(MyApplication.getOwnUser().getUser_ID()));
                                userJoinRoom.setJoin_Role("成员");
                                userJoinRoom.setJoin_Time(TimeUtil.currentTime());
                                userJoinRoom.setRoom_ID(room.getRoom_ID());
                                joinRoom.setUserJoinRoom(userJoinRoom);
                                joinRoom.join();
                            }
                        }).create().show();

                return false;
            }
        });

    }

    // 查找房间
    private void searchRoom() {

        String str_searchTerm = et_roomName.getText().toString();
        if (str_searchTerm.isEmpty()) return;
        searchRoom.setSearchTerm(str_searchTerm);
        searchRoom.search();
    }
}
